'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url admin/dls/kh/dytflj/batchRun_tflj 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			tfid,
			todayStart,
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		console.log("开始添加 tfid===========================", tfid);
		//修改投放链接的数据 
		let all_user = await vk.baseDao.count({
			dbName: "uni-id-users",
			whereJson: {
				tfid: tfid,
			}
		});

		let today_user = await vk.baseDao.count({
			dbName: "uni-id-users",
			whereJson: {
				tfid: tfid,
				register_date: _.gt(todayStart)
			}
		});

		let all_tfAmount = await vk.baseDao.sum({
			dbName: "a_recharge",
			fieldName: "shifu_amount", // 需要求和的字段名
			whereJson: {
				tfid: tfid,
				pay_status: 1,
			}
		});

		let all_tfUser = await vk.baseDao.selects({
			dbName: "a_recharge",
			pageIndex: 1,
			pageSize: 1000,
			whereJson: {
				tfid: tfid,
				pay_status: 1,
			},
			groupJson: {
				// _id是分组id， 将$register_date转为date，然后将date转为需要分组的date格式，直接分组
				_id: "$user_id",
			}
		});

		let all_tfUsers = all_tfUser.rows.length

		let all_userPay = await vk.baseDao.count({
			dbName: "a_recharge",
			whereJson: {
				tfid: tfid,
				pay_status: 1,
			}
		});



		let today_tfAmount = await vk.baseDao.sum({
			dbName: "a_recharge",
			fieldName: "shifu_amount", // 需要求和的字段名
			whereJson: {
				tfid: tfid,
				pay_status: 1,
				_add_time: _.gt(todayStart)
			}
		});

		let today_tfUser = await vk.baseDao.selects({
			dbName: "a_recharge",
			pageIndex: 1,
			pageSize: 1000,
			whereJson: {
				tfid: tfid,
				pay_status: 1,
				_add_time: _.gt(todayStart)
			},
			groupJson: {
				// _id是分组id， 将$register_date转为date，然后将date转为需要分组的date格式，直接分组
				_id: "$user_id",
			}
		});

		let today_tfUsers = today_tfUser.rows.length

		let today_userPay = await vk.baseDao.count({
			dbName: "a_recharge",
			whereJson: {
				tfid: tfid,
				pay_status: 1,
				_add_time: _.gt(todayStart)
			}
		});

		let add_tfurl = await vk.baseDao.update({
			dbName: "a_dy_tfurl", // 表名
			whereJson: { // 条件
				_id: tfid
			},
			dataJson: { // 需要修改的数据
				all_user: all_user,
				today_user: today_user,
				all_tfAmount: all_tfAmount,
				all_tfUser: all_tfUsers,
				all_userPay: all_userPay,
				today_tfAmount: today_tfAmount,
				today_tfUser: today_tfUsers,
				today_userPay: today_userPay
			}
		});
		console.log("添加成功");



		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}